using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class TemplateControl : BaseItem
    {
        private TemplateControl() : base() { }
        private TemplateControl(Hashtable table) : base(table) { }
        private TemplateControl(Hashtable table, List<TemplateControl> collection) : base(table, collection) { }

        protected override Base CreateDal()
        {
            return new TemplateControlDal();
        }
        
        public static TemplateControl CreateInstance()
        {
            TemplateControl tmp = new TemplateControl();
            tmp.Sequence = -1;
            return tmp;
        }

        private static TemplateControl GetItem(Hashtable table)
        {
            if (table != null)
                return new TemplateControl(table);
            else
                return null;
        }

        private static List<TemplateControl> GetItems(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<TemplateControl> tmp = new List<TemplateControl>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new TemplateControl(table, tmp));
                }
                return tmp;
            }
            else
            {
                return null;
            }
        }

        public static TemplateControl GetById(int id)
        {
            TemplateControlDal dal = new TemplateControlDal();
            return GetItem(dal.GetById(id));
        }

        public static List<TemplateControl> GetAllByTemplateId(int templateId)
        {
            TemplateControlDal dal = new TemplateControlDal();
            return GetItems(dal.GetAllByTemplateId(templateId));
        }

        protected override void Load(Hashtable table)
        {
            id = (int)table[TemplateControlDal.Fields.Id];
            templateId = (int)table[TemplateControlDal.Fields.TemplateId];
            name = (string)table[TemplateControlDal.Fields.Name];
            typeId = (int)table[TemplateControlDal.Fields.TypeId];
            typeName = (string)table[TemplateControlDal.Fields.TypeName];
            adminControlType = (string)table[TemplateControlDal.Fields.AdminControlType];
            Notes = (string)table[TemplateControlDal.Fields.Notes];
            Sequence = (int)table[TemplateControlDal.Fields.Sequence];
        }

        private int id;
        public int Id
        {
            get { return id; }
        }

        private int templateId;
        public int TemplateId
        {
            get { return templateId; }
            set { templateId = value; }
        }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        
        private int typeId;
        public int TypeId
        {
            get { return typeId; }
            set { typeId = value; }
        }

        private string typeName;
        public string TypeName
        {
            get { return typeName; }
        }

        private string adminControlType;
        public string AdminControlType
        {
            get { return adminControlType; }
        }

        public string Notes { get; set; }

        public int Sequence { get; set;}

        public new void Update()
        {
            base.Update();
        }

        protected override object SaveChanges(ref object concurencyId, Base dal)
        {
            int sequenceOut = Sequence;
            ((TemplateControlDal)dal).Update(ref id, templateId, name, typeId, ref typeName, ref adminControlType, Notes, ref sequenceOut, ref concurencyId);
            Sequence = sequenceOut;
            return id;
        }

        public new void Delete()
        {
            base.Delete();
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
            ((TemplateControlDal)dal).Delete(id, concurencyId);
        }

        public static void Move(List<TemplateControl> collection, int movingIndex, int replacingIndex)
        {
            //get the moving
            TemplateControl moving = collection[movingIndex];
            //get the move to
            TemplateControl replacing = collection[replacingIndex];

            bool saveAll = false;
            if (moving.Sequence == replacing.Sequence)
            {
                saveAll = true;
                for (int i = 0; i < collection.Count; i++)
                {
                    if (collection[i].Sequence != i)
                    {
                        collection[i].Sequence = i;
                    }
                }
            }

            int tmpSequence = moving.Sequence;
            moving.Sequence = replacing.Sequence;
            replacing.Sequence = tmpSequence;

            if (saveAll)
            {
                collection[0].BeginTransaction();
                foreach (TemplateControl t in collection)
                {
                    if (t != collection[0])
                    {
                        t.UseTransaction(collection[0].TransactionKey);
                    }
                    t.Update();
                }
                collection[0].CommitTransaction();
            }
            else
            {
                moving.BeginTransaction();
                replacing.UseTransaction(moving.TransactionKey);
                moving.Update();
                replacing.Update();
                moving.CommitTransaction();
            }

        }
    }
}
